//
//  LoginViewController.m
//  MyFishHelper
//
//  Created by EmberWei on 2018/6/15.
//  Copyright © 2018年 EmberWei. All rights reserved.
//

#import "LoginViewController.h"
#import "HomeViewController.h"

@interface LoginViewController ()<LLSwitchDelegate>
@property (nonatomic,strong) NSArray *textFieldArr;
@property (nonatomic,strong) YJJTextField *userNameField;
@property (nonatomic,strong) YJJTextField *passwordField;
@property (nonatomic,strong) UILabel *serverLabel;
@property (nonatomic,strong) Account *account;
@property (nonatomic,strong) LLSwitch *llSwitch;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self loadAccount];
    [[UITextField appearance] setTintColor:[UIColor whiteColor]];
}

- (void)setupUI
{
    self.title = @"登录";
    YJJTextField *userNameField = [YJJTextField yjj_textField];
    userNameField.frame = CGRectMake(20,64+ 100, self.view.frame.size.width - 40, 80);
    userNameField.textColor = [UIColor whiteColor];
    userNameField.placeHolderColor = [UIColor whiteColor];
    userNameField.textLengthLabelColor = [UIColor whiteColor];
    userNameField.maxLength = 11;
    userNameField.textField.keyboardType = UIKeyboardTypePhonePad;
    userNameField.errorStr      = @"手机长度不得超过11位";
    userNameField.placeholder   = @"请输入手机号";
    userNameField.leftImageName = @"username_login";
    userNameField.backgroundColor = [UIColor clearColor];
    [self.view addSubview:userNameField];
    self.userNameField = userNameField;
    
    YJJTextField *passwordField = [YJJTextField yjj_textField];
    passwordField.frame = CGRectMake(20,64+ 180, self.view.frame.size.width - 40, 80);
    passwordField.maxLength = 20;
    passwordField.textField.secureTextEntry = YES;
    passwordField.errorStr = @"密码长度不得超过20位";
    passwordField.placeholder = @"请输入密码";
    passwordField.leftImageName = @"password_login";
    passwordField.showHistoryList = NO;
    passwordField.textColor = [UIColor whiteColor];
    passwordField.placeHolderColor = [UIColor whiteColor];
    passwordField.backgroundColor = [UIColor clearColor];
    passwordField.textLengthLabelColor = [UIColor whiteColor];
    [self.view addSubview:passwordField];
    self.passwordField = passwordField;
    self.textFieldArr = @[userNameField,passwordField];
    
    
    TYFWaveButton *button = [TYFWaveButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(50, 64+ 180 + 100, self.view.frame.size.width - 100, 50);
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.radiusofWaveStart = 40;
    button.radiusofWaveEnd = 140;
    button.colorofwave = [UIColor whiteColor];
    button.timeofwave = 1.0;
    button.onlycenter = YES;
    button.layer.masksToBounds = YES;
    button.layer.cornerRadius = 4.0f;
    [button setBackgroundColor:HEXCOLOR(0x01B7A4)];
    [button addTarget:self action:@selector(actionLogin:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    
    LLSwitch *llSwitch = [[LLSwitch alloc] initWithFrame:CGRectMake(button.frame.origin.x + button.frame.size.width - 60, 64+ 180 + 100 + 80, 60, 30)];
    llSwitch.onColor = HEXCOLOR(0x01B7A4);           // switch is open color    开关打开的颜色
    llSwitch.offColor = [UIColor lightGrayColor];    // switch is close color    开关关闭的颜色
    llSwitch.faceColor = [UIColor whiteColor];       // switch face color    圆脸的颜色
    llSwitch.animationDuration = 0.5f;    // switch open or close animation time    开关的动画时间
    [llSwitch setOn:NO];
    llSwitch.delegate = self;
    [self.view addSubview:llSwitch];
    self.llSwitch = llSwitch;
    
    self.serverLabel = [[UILabel alloc]initWithFrame:CGRectMake(llSwitch.frame.origin.x - 80, 64+ 180 + 100 + 80, 60, 30)];
    self.serverLabel.text = @"测试服";
    self.serverLabel.textColor = [UIColor whiteColor];
    [self.view addSubview:self.serverLabel];
    
}

-(void)loadAccount {
    Account *account = [Account loadCache];
    if (account) {
        self.userNameField.textField.text = account.mobile;
        self.passwordField.textField.text = account.password;
        [self.llSwitch setOn:account.isProduct];
        [Singleton shareInstance].isProduct = account.isProduct;
        self.serverLabel.text = account.isProduct ? @"正式服" : @"测试服";
    }
}

-(void)actionLogin:(UIButton*)button {
    NSString *moblie = self.userNameField.textField.text;
    NSString *password = self.passwordField.textField.text;
    if (moblie.length == 0 ) {
        [TSMessage showNotificationWithTitle:@"输入错误"
                                    subtitle:@"请输入手机号"
                                        type:TSMessageNotificationTypeError];
        return;
    }
    if (moblie.length != 11) {
        [TSMessage showNotificationWithTitle:@"输入错误"
                                    subtitle:@"手机号必须为11位数字"
                                        type:TSMessageNotificationTypeError];
        return;
    }
    if (password.length == 0) {
        [TSMessage showNotificationWithTitle:@"输入错误"
                                    subtitle:@"请输入正确的密码"
                                        type:TSMessageNotificationTypeError];
        return;
    }
    WEAK(self)
    [Api apiLogin:moblie password:password handler:^(ResponseLogin *result, NSError *error) {
        STRONG(self)
        if (error) {
            [TSMessage showNotificationWithTitle:@"网络错误"
                                        subtitle:@"请检查网络,稍后再试"
                                            type:TSMessageNotificationTypeError];
            return;
        }
        if (![result.code isEqualToString:@"0"]) {
            [TSMessage showNotificationWithTitle:@"错误"
                                        subtitle:result.msg
                                            type:TSMessageNotificationTypeError];
            return;
        }
        Token *token = result.data;
        if (token.token.length > 0) {
            [token saveCache];
            [self saveAccount];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                UIStoryboard *home = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
                [self presentViewController:home.instantiateInitialViewController animated:YES completion:nil];
            });
        }
    }];
}

-(void)saveAccount {
    self.account = [[Account alloc]init];
    self.account.mobile = self.userNameField.textField.text;
    self.account.password = self.passwordField.textField.text;
    self.account.isProduct = self.llSwitch.on;
    [Singleton shareInstance].isProduct = self.account.isProduct;
    [self.account saveCache];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for (YJJTextField *textField in self.textFieldArr) {
        [textField setPlaceHolderLabelHidden:YES];
        [textField dismissTheHistoryContentTableView];
    }
    [self.view endEditing:YES];
}


- (void)valueDidChanged:(LLSwitch *)llSwitch on:(BOOL)on {
    self.account = [Account loadCache];
    if (self.account == nil) {
        self.account = [[Account alloc]init];
    }
    self.account.isProduct = self.llSwitch.on;
    [Singleton shareInstance].isProduct = self.account.isProduct;
    [self.account saveCache];
    self.serverLabel.text = on ? @"正式服" : @"测试服";
}

@end
